package com.tlp.tlp2k11;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class HelloAndroid extends Activity implements OnClickListener {

	private static final String HOLA_TLP_2K11 = "HOLA TLP 2k11";
	
	private boolean exit = false;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hello);
		showToast("CREATE!!!");
		
		View btnPhoto = findViewById(R.id.buttonPhoto);
		btnPhoto.setOnClickListener(this);
		View btnAccelerometer = findViewById(R.id.buttonAccelerometer);
		btnAccelerometer.setOnClickListener(this);
	}
	
	private void showToast(String msg) {
		Context context = getApplicationContext();
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(context, msg, duration);
		toast.setGravity(Gravity.CENTER, 10, 10);
		toast.show();
	}
	
	@Override
	protected void onRestart() {
		showToast("RESTART!!!");
		super.onRestart();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
			exit = true;

		return super.onKeyDown(keyCode, event);
	}
	
	private void sendNotification(String contentText) {
		NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
		int icon = android.R.drawable.ic_media_pause;
		String tickerText = HOLA_TLP_2K11;
		Context context = getApplicationContext();
		String contentTitle = "¿Cómo va la cosa?";
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, this.getIntent(), 0);
		Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		
		notification.defaults |= Notification.DEFAULT_VIBRATE;

		manager.notify(0, notification);
	}
	
	@Override
	protected void onStop() {
		if (!exit)
			sendNotification("Ceda el paso");
		
		showToast("STOP!!!");
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		showToast("DESTROY!!!");
		super.onDestroy();
	}

	public void onClick(View v) {
		exit = true;
		switch (v.getId()) {
		case R.id.buttonPhoto:
			Intent intentPhoto = new Intent(this, PhotoCapture.class);
			startActivity(intentPhoto);
			return;
		case R.id.buttonAccelerometer:
			Intent intentAccelerometer = new Intent(this, AccelerometerTlp.class);
			startActivity(intentAccelerometer);
			return;
		}		
	}

}
